<%--                                                                       --%>
<%-- Copyright (c) 2003-2005 BroadVision, Inc.  All rights reserved.       --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>

<%@ page import = "java.util.*, java.net.*,
  com.broadvision.ssp.client.SelfServiceProcess,
  com.broadvision.ssp.util.TableConstants"
%>

<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/bv-html.tld" prefix="bvhtml" %>
<%@ include file="/mc/common/encodeURL.jsp" %>

<script language="javascript" src="<%=request.getContextPath()%>/mt/js/date.js"></script>
<script language="javascript" src="<%=request.getContextPath()%>/bp/js/user_popup_funcs.js"></script>

<SCRIPT language="javascript">
  var definitionVersionTextArray;
  var definitionVersionValueArray;
  var definitionVersionSelectedValue;
  var lastProcessChoice = <bean:write name="reportCriteriaForm" property="processChoice"/>;

  function backupVersion(field) {
    var len = field.length;
    if (len > 1) {
      definitionVersionTextArray = new Array(len);
      definitionVersionValueArray = new Array(len);
      var i = 0;
      for (i = 0; i < len; i++) {
        definitionVersionTextArray[i] = field[i].text;
        definitionVersionValueArray[i] = field[i].value;
        if (field[i].selected == true) {
          definitionVersionSelectedValue = field[i].value;
        }
      }
    }
  }
  
  function restoreVersion(field) {
    if (definitionVersionValueArray == null) {
      return;
    }
    var len = definitionVersionValueArray.length;
    if (len > 1) {
      field.length = len;
      var i = 0;
      for (i = 0; i < len; i++) {
        var selectedOption = (definitionVersionValueArray[i] == definitionVersionSelectedValue);
        var newoption = new Option(definitionVersionTextArray[i], 
          definitionVersionValueArray[i], selectedOption, selectedOption);
        field[i] = newoption;
      }
      field.disabled = false;
    }
  }
  
  function setUserAlias(formName, fieldName, fieldValue) {
    // this page does not have any hidden fields that rely on the popup,
    // we do not need a refresh url.
    var refreshUrl = null;
    setFieldValue(refreshUrl, formName, fieldName, fieldValue);
    var thisForm = document.forms[formName];
    if (thisForm != null && fieldName == 'managerAlias') {
      lastProcessChoice = 2;
      thisForm.processChoice[2].checked = true; 
      backupVersion(thisForm.definitionVersion);
      thisForm.definitionVersion.length = 0;
      thisForm.definitionVersion.disabled = true;
    }
    if (thisForm != null && fieldName == 'participantAlias') {
      ; // do anything special???
    }
  }

  function doCriteria(myform) {
    myform.target = "_self";
    myform.action = '<c:url value="${reportCriteriaForm.map.criteriaAction}"/>';
    myform.submit();
    return false;
  }

  function doCancel(myform) {
    myform.target = "_self";
    myform.action = '<c:url value="${reportCriteriaForm.map.listAction}"/>';
    myform.submit();
    return false;
  }

  function doExportReport(formName) {
    var myform = document.forms[formName];
    if (myform == null) {
      return false;
    }
    if (doValidate(myform) == false) {
      return false;
    }
    myform.doExport.value = true;
    myform.forwardAction.value = "export";
    doCriteria(myform);
    myform.forwardAction.value = "";
    myform.target = "_self";
    myform.doExport.value = false;
  }
  
  function doExportReportFinal(formName) {
    var myform = document.forms[formName];
    if (myform == null) {
      return false;
    }
    myform.forwardAction.value = "";
    myform.target = "_self";
    myform.doExport.value = false;
  }
  
  function doNothing() {
  }

  function doShowReport(myform) {
    if (doValidate(myform) == false) {
      return false;
    }
    myform.doExport.value = false;
    myform.forwardAction.value = "report";
    doCriteria(myform);
  }
  
  function doShowReportFinal(formName) {
    var myform = document.forms[formName];
    if (myform == null) {
      return false;
    }
    myform.doExport.value = false;
    myform.forwardAction.value = "";
    var winName = getTimeStamp();
    myform.reportId.value = winName;
    bparam = 'reportSubjectNameKey=' + '<bean:write name="reportCriteriaForm" property="reportSubjectNameKey"/>';
    var str = encodeURL('bp/reportWaiting.do', bparam);
    var reportWin = window.open(str, winName, 'directories=no,height=550,width=750,left=0,top=0,resizable=yes,scrollbars=yes,location=no,menubar=no,status=no,toolbar=no');
    var waitCount = 0;
    while (reportWin.closed == false && reportWin.doneLoading == null) {
      setTimeout('doNothing()', 100);
      waitCount += 1;
      if (waitCount >= 30) { // wait for 3 seconds (100 X 30 = 3000 msec = 3 sec) max
        break;
      }
    }
    if (reportWin.closed == true) {
      return false;
    }
    myform.target = winName;
    myform.action = '<c:url value="${reportCriteriaForm.map.reportAction}"/>';
    myform.submit();
    return false;
  }
  
  function doRadioAllModels(myform) {
    lastProcessChoice = 0;
    if (myform.definitionVersion.length > 1) {
      backupVersion(myform.definitionVersion);
      myform.definitionVersion.length = 0;
      myform.definitionVersion.disabled = true;
    }
  }
  
  function doRadioSpecificModel(myform) {
    if (myform.processOid.value > 0) {
      lastProcessChoice = 1;
      restoreVersion(myform.definitionVersion);
      return true;
    }
    else {
      myform.processChoice[lastProcessChoice].checked = true;
      doModel(myform);
      return false;
    }
  }
  
  function doRadioManager(myform) {
    if (myform.managerAlias == null || myform.managerAlias.value.length == 0 || myform.managerAlias.value == "") {
      findUserPopup('<html:rewrite page="/mt/userSearchPopup.do"/>', 'reportCriteriaForm', 'managerAlias');
    }
    if (myform.managerAlias == null || myform.managerAlias.value.length == 0 || myform.managerAlias.value == "") {
      myform.processChoice[lastProcessChoice].checked = true;
      return false;
    }
    else {
      lastProcessChoice = 2;
      backupVersion(myform.definitionVersion);
      myform.definitionVersion.length = 0;
      myform.definitionVersion.disabled = true;
      return true;
    }
  }

  function doTextManager(myform) {
    if (myform.managerAlias != null && myform.managerAlias.value.length > 0) {
      lastProcessChoice = 2;
      myform.processChoice[2].checked = true; 
      backupVersion(myform.definitionVersion);
      myform.definitionVersion.length = 0;
      myform.definitionVersion.disabled = true;
    }
  }
  
  function onSelection(param)
  {
    var firstSplit = param.split('&');
    var oidSplit = firstSplit[0].split('=');
    var pathSplit = firstSplit[1].split('=');
    if (document.reportCriteriaForm.processOid.value != oidSplit[1]) {
      document.reportCriteriaForm.definitionVersion.value = -1;
    }
    document.reportCriteriaForm.processOid.value = oidSplit[1];
    document.reportCriteriaForm.processName.value = pathSplit[1];
    document.reportCriteriaForm.processChoice[1].checked = true;
    lastProcessChoice = 1;
    document.reportCriteriaForm.target = "_self";
    document.reportCriteriaForm.action = '<c:url value="${reportCriteriaForm.map.criteriaAction}"/>';
    document.reportCriteriaForm.submit();
    return true;
  }
  
  function doModel(myform) {
    var bparam = "contentTypeId=<%=TableConstants.SSP_PROCESS_CONTENT_TYPE%>";
    bparam += "&selectItemOnly=on&includeContent=on&categoryID=0&action=expandCategory&rootNodeOID=0";
    bparam += "&filterClass=com.broadvision.process.tools.action.process.ProcessContentFilter";
    if (myform.processOid.value > 0) {
      bparam += "&currentNodeId=" + myform.processOid.value;
    }
    var str = encodeURL('bp/allCategoryBrowser.do', bparam);
    var qualifier = window.open(str, "bpmCategoryAndContentPicker",
      "directories=no,height=300,width=450,left=50,top=50,resizable=yes,scrollbars=yes,location=no,menubar=no,status=no,toolbar=no,");
    if (qualifier != null) {
        if (qualifier.opener == null)
          qualifier.opener = self;
        qualifier.focus();
    }
  }
  
  function doValidate(myform) {
    // validate process oid, version, manager, date range, and show number
    if (myform.processChoice[1].checked == true && myform.definitionVersion[0].selected == true) {
      alert("<bean:message key="bp.report.criteria.validation.versionNumber" bundle="bp.messages"/>");
      myform.definitionVersion.focus();
      return false;
    }
    if (<bean:write name="reportCriteriaForm" property="isProcessAdmin"/> == true) {
      if (myform.processChoice[2].checked == true && 
        (myform.managerAlias == null || myform.managerAlias.value.length == 0 || myform.managerAlias.value == "")) {
        alert("<bean:message key="bp.report.criteria.validation.managerAlias" bundle="bp.messages"/>");
        myform.managerAlias.focus();
        return false;
      }
    }
    if (<bean:write name="reportCriteriaForm" property="hasDateRange"/> == true) {
      var dateFrom = getDateOnly("reportCriteriaForm", "dateFrom");
      if (dateFrom == null) {
        alert("<bean:message key="bp.report.criteria.validation.dateFrom" bundle="bp.messages"/>");
        return false;
      }
      var dateTo = getDateOnly("reportCriteriaForm", "dateTo");
      if (dateTo == null) {
        alert("<bean:message key="bp.report.criteria.validation.dateTo" bundle="bp.messages"/>");
        return false;
      }
      if (dateTo < dateFrom) {
        alert("<bean:message key="bp.report.criteria.validation.dateToEarlierThanDateFrom" bundle="bp.messages"/>");
        return false;
      }
    }
    if (<bean:write name="reportCriteriaForm" property="hasParticipant"/> == true) {
      var participant = myform.participantAlias.value;
      if (participant.length == 0) {
        alert("<bean:message key="bp.report.criteria.validation.participantAlias" bundle="bp.messages"/>");
        myform.participantAlias.select();
        myform.participantAlias.focus();
        return false;
      }
    }
    var entriesPerPageString = myform.entriesPerPage.value;
    var len = entriesPerPageString.length;
    var i = 0;
    for (i = 0; i < len; i++) {
      if (entriesPerPageString.charAt(i) < '0' || entriesPerPageString.charAt(i) > '9') {
        alert("<bean:message key="bp.report.criteria.validation.entriesPerPage" bundle="bp.messages"/>");
        myform.entriesPerPage.select();
        myform.entriesPerPage.focus();
        return false;
      }
    }
    if (myform.entriesPerPage.value < 1 || myform.entriesPerPage.value > 100) {
      alert("<bean:message key="bp.report.criteria.validation.entriesPerPage" bundle="bp.messages"/>");
      myform.entriesPerPage.select();
      myform.entriesPerPage.focus();
      return false;
    }
    return true;
  }
  
  function findProcessChoiceRadioSelection(myform) {
    var i = 0;
    var foundIndex = -1;
    for (i = 0; i < myform.processChoice.length; i++) {
      if (myform.processChoice[i].checked == true) {
        foundIndex = i;
        break;
      }
    }
    return foundIndex;
  }
  
  function checkNumberOnly(e) {
    var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
      return false;
    }
    else {
      return true;
    }
  }

</SCRIPT>

<html:form action="/bp/reportCriteria.do" onsubmit="doShowReport(reportCriteriaForm); return false;">
<html:hidden name="reportCriteriaForm" property="reportSubjectNameKey"/>
<html:hidden name="reportCriteriaForm" property="reportDescriptionKey"/>
<html:hidden name="reportCriteriaForm" property="dateRangeKey"/>
<html:hidden name="reportCriteriaForm" property="criteriaAction"/>
<html:hidden name="reportCriteriaForm" property="listAction"/>
<html:hidden name="reportCriteriaForm" property="reportAction"/>
<html:hidden name="reportCriteriaForm" property="forwardAction"/>
<html:hidden name="reportCriteriaForm" property="isProcessAdmin"/>
<html:hidden name="reportCriteriaForm" property="processOid"/>
<html:hidden name="reportCriteriaForm" property="hasParticipant"/>
<html:hidden name="reportCriteriaForm" property="hasDateRange"/>
<html:hidden name="reportCriteriaForm" property="doExport"/>
<html:hidden name="reportCriteriaForm" property="sortBy"/>
<html:hidden name="reportCriteriaForm" property="reportId"/>
<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
  <tr>
    <td valign="top" width="100%">
      <div class="pageTitle">
        <bean:message name="reportCriteriaForm" property="reportSubjectNameKey" bundle="bp.messages"/>
      </div> 
      <div class="errorMessage"><html:errors bundle="bp.messages"/></div>
      <div class="pageInstructions">
        <bean:message name="reportCriteriaForm" property="reportDescriptionKey" bundle="bp.messages"/>
      </div> 
      <div class="pageSubtitle">
        <span class="left"><bean:message key="bp.report.criteria.reportingCriteria" bundle="bp.messages"/></span>
        <span class="right">&nbsp;</span>
      </div> 
      <table class="labelForm" width="100%" border="0" cellspacing="0" cellpadding="0" summary="Reporting criterias">
        <tr> 
          <td class="label" width="30%">
            <bean:message key="bp.report.criteria.processModel" bundle="bp.messages"/>
          </td>
          <td class="labelField" width="70%" colspan="2">
            <html:radio name="reportCriteriaForm" property="processChoice" value="0"
              onclick="doRadioAllModels(this.form);"/>
            <logic:equal name="reportCriteriaForm" property="isProcessAdmin" value="true">
              <bean:message key="bp.report.criteria.onAllModels" bundle="bp.messages"/>
            </logic:equal>
            <logic:equal name="reportCriteriaForm" property="isProcessAdmin" value="false">
              <bean:message key="bp.report.criteria.onAllMyModels" bundle="bp.messages"/>
            </logic:equal>
            <br>
            <html:radio name="reportCriteriaForm" property="processChoice" value="1"
              onclick="return doRadioSpecificModel(this.form);"/>
            <bean:message key="bp.report.criteria.onASpecificModel" bundle="bp.messages"/>
            <html:text name="reportCriteriaForm" property="processName" size="20" maxlength="80" readonly="true"/>
            <input type="button" name="browseModel" class="pageButton"
              onclick="doModel(this.form);"
              value='<bean:message key="button.browse" bundle="bp.messages"/>'>
            <logic:equal name="reportCriteriaForm" property="isProcessAdmin" value="true">
              <br>
              <html:radio name="reportCriteriaForm" property="processChoice" value="2"
                onclick="return doRadioManager(this.form);"/>
              <bean:message key="bp.report.criteria.onModelsManagedBy" bundle="bp.messages"/>
              <html:text name="reportCriteriaForm" property="managerAlias" size="20" maxlength="80" onchange="doTextManager(this.form)"/>
              <input type="button" name="browseManager" class="pageButton"
                onClick="JavaScript:findUserPopup('<html:rewrite page="/mt/userSearchPopup.do"/>', 'reportCriteriaForm', 'managerAlias');"
                value='<bean:message key="button.browse" bundle="bp.messages"/>'>
            </logic:equal>
          </td>
        </tr>
        <tr> 
          <td class="label" width="30%">
            <bean:message key="bp.report.criteria.modelVersion" bundle="bp.messages"/>
          </td>
          <td class="labelField" width="70%" colspan="2">
            <logic:notEmpty name="reportCriteriaForm" property="definitionVersionList">
              <html:select name="reportCriteriaForm" property="definitionVersion" size="1">
                <html:option key="bp.report.criteria.selectOption" value="-1" bundle="bp.messages"/>
                <html:optionsCollection name="reportCriteriaForm" property="definitionVersionList"/>
              </html:select>
            </logic:notEmpty>
            <logic:empty name="reportCriteriaForm" property="definitionVersionList">
              <html:select name="reportCriteriaForm" property="definitionVersion" size="1" disabled="true">
              </html:select>
            </logic:empty>
          </td>
        </tr>
        <logic:equal name="reportCriteriaForm" property="hasParticipant" value="true">
          <tr> 
            <td class="label" width="30%">
              <bean:message key="bp.report.criteria.participant" bundle="bp.messages"/>
            </td>
            <td class="labelField" width="70%" colspan="2">
              <html:text name="reportCriteriaForm" property="participantAlias" size="20" maxlength="80"/>
              <input type="button" name="browseParticipant" class="pageButton"
                onClick="JavaScript:findUserPopup('<html:rewrite page="/mt/userSearchPopup.do"/>', 'reportCriteriaForm', 'participantAlias')"
                value='<bean:message key="button.browse" bundle="bp.messages"/>'>
            </td>
          </tr>
        </logic:equal>
        <logic:equal name="reportCriteriaForm" property="hasDateRange" value="true">
          <tr> 
            <td class="label" width="30%">
              <bean:message name="reportCriteriaForm" property="dateRangeKey" bundle="bp.messages"/>
            </td>
            <td class="labelField" width="10%" nowrap>
              <bean:message key="bp.report.criteria.dateFrom" bundle="bp.messages"/>
            </td>
            <td class="labelField" width="60%" nowrap>
              <bvhtml:date name="reportCriteriaForm" property="dateFrom"
                 onChangeMonth="onChangeMonth" onChangeYear="onChangeYear"/>
              <a href="JavaScript:datePopup('<html:rewrite page="/mt/calendar.do"/>', 'reportCriteriaForm', 'dateFrom')">
                <img src="<%=request.getContextPath()%>/bp/images/action/calendar.gif" width="23" height="22" border="0" align="absmiddle" alt="<bean:message key="image.alt.chooseDate" bundle="bp.messages"/>"></a>
              <html:hidden property="dateFrom.hour" value="0"/>
              <html:hidden property="dateFrom.minute" value="0"/>
              <html:hidden property="dateFrom.ind" value="0"/>
            </td>
          </tr>
          <tr>
            <td class="label" width="30%">&nbsp;</td>
            <td class="labelField" width="10%" nowrap>
              <bean:message key="bp.report.criteria.dateTo" bundle="bp.messages"/>
            </td>
            <td class="labelField" width="60%" nowrap>
              <bvhtml:date name="reportCriteriaForm" property="dateTo"
                 onChangeMonth="onChangeMonth" onChangeYear="onChangeYear"/>
              <a href="JavaScript:datePopup('<html:rewrite page="/mt/calendar.do"/>', 'reportCriteriaForm', 'dateTo')">
                <img src="<%=request.getContextPath()%>/bp/images/action/calendar.gif" width="23" height="22" border="0" align="absmiddle" alt="<bean:message key="image.alt.chooseDate" bundle="bp.messages"/>"></a>
              <html:hidden property="dateTo.hour" value="0"/>
              <html:hidden property="dateTo.minute" value="0"/>
              <html:hidden property="dateTo.ind" value="0"/>
            </td>
          </tr>
        </logic:equal>
        <tr> 
          <td class="label" width="30%">
            <bean:message key="bp.report.criteria.show" bundle="bp.messages"/>
          </td>
          <td class="labelField" width="70%" colspan="2">
            <html:text name="reportCriteriaForm" property="entriesPerPage" size="10" maxlength="80" onkeypress="return checkNumberOnly(event)"/>
            <bean:message key="bp.report.criteria.itemsPerPage" bundle="bp.messages"/>
          </td>
        </tr>
        <tr> 
          <td class="label" width="30%">
            &nbsp;
          </td>
          <td class="labelField" width="70%" colspan="2">
            <bean:message key="bp.report.criteria.specifyShowNumberInstruction" bundle="bp.messages"/>
          </td>
        </tr>
      </table>
      <div class="pageButtonWrap" align="right">
        <html:link href="#" onclick="javascript:doExportReport('reportCriteriaForm');">
          <img src="<%=request.getContextPath()%>/bp/images/icons/export_data.gif" width="20" height="20" border="0" alt='<bean:message key="image.alt.exportReport" bundle="bp.messages"/>' title='<bean:message key="image.alt.exportReport" bundle="bp.messages"/>' align="absmiddle">
        </html:link>
        <html:link href="#" onclick="javascript:doExportReport('reportCriteriaForm');">
          <bean:message key="bp.report.criteria.exportReport" bundle="bp.messages"/>
        </html:link>
        &nbsp;&nbsp;&nbsp;
        <input type="button" name="selectAllProcesses" class="pageButton" onclick="javascript:doCancel(this.form);"
          value='<bean:message key="button.cancel" bundle="bp.messages"/>'>
        <html:submit property="showReport" styleClass="pageButton">
          <bean:message key="bp.report.criteria.showReport" bundle="bp.messages"/>
        </html:submit>
      </div>
    </td>
  </tr>
</table>
<script language="Javascript1.2">
  var forwardActionString = '<bean:write name="reportCriteriaForm" property="forwardAction"/>';
  if (forwardActionString != null && forwardActionString.length > 0) {
    if (forwardActionString == 'export') {
      doExportReportFinal('reportCriteriaForm');
    }
    else if (forwardActionString == 'report') {
      doShowReportFinal('reportCriteriaForm');
    }
  }
  var choice = <bean:write name="reportCriteriaForm" property="processChoice"/>;
  if (choice != 1) {
    var theform = document.forms['reportCriteriaForm'];
    if (theform != null && theform.definitionVersion != null) {
      backupVersion(theform.definitionVersion);
      theform.definitionVersion.length = 0;
      theform.definitionVersion.disabled = true;
    }
  }
</script>
</html:form>

